package com.etoak.rst.sys.user.controller;


import com.etoak.rst.sys.user.DTO.UserDto;
import com.etoak.rst.sys.user.service.IUserService;
import com.etoak.rst.sys.util.JsonResponse;
import com.etoak.rst.sys.util.RedisUtil;
import com.etoak.rst.sys.util.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private IUserService service;
    @RequestMapping("/Login")
    public JsonResponse Login(UserDto dto) throws JsonProcessingException {
        JsonResponse response = null;
        //后台判断
        if(!StringUtils.isNullOrEmpty(dto.getUsername())&& !StringUtils.isNullOrEmpty(dto.getPassword())){
            UserDto user = service.Login(dto);
            if( user!=null && user.getUsername()!=null ){
                //创建token
                String token = UUID.randomUUID().toString().replaceAll("-","");
                JsonMapper mapper = new JsonMapper();
                String userinfo = mapper.writeValueAsString(user);
                //存入redis中
                RedisUtil ru = RedisUtil.getRedisUtil();
                ru.setex(token,18000,userinfo);
                user.setToken(token);
                return Result.success("200","success",user);
            }else{
                response = Result.failed("20002","failed","用户名或者密码错误");
            }
        }else{
            response = Result.failed("20001","failed","用户名或者密码不能为空");
        }

        return response;

    }


    @RequestMapping("/addPic")
    public JsonResponse addPic(MultipartFile pic){
        String oldName = pic.getOriginalFilename();
        String pest = oldName.substring(oldName.lastIndexOf("."));
        String newName = UUID.randomUUID().toString().replaceAll("-","")+pest;

        File file = new File("D:\\taspics\\" + newName);
        if( file.exists() ) throw new RuntimeException("已经存在");
        try {
            pic.transferTo(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

//        O o = new O();
//        o.setSavepath(newName);
//        o.setRealname(oldName.substring(0,oldName.lastIndexOf(".")));
//        o.setUploadtime(new Date());

        return Result.success("200","success","/pics/"+newName);
    }

    @RequestMapping("/Register")
    public JsonResponse Register(@RequestBody UserDto dto){
        boolean flag = service.Register(dto);
        return flag?Result.success("200","success","注册成功")
                :Result.failed("20001","failed","注册失败");
    }

}
